package com.ykht.proj.comsrv.utils.msgutil;

import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.time.Instant;

@Slf4j
@Component
@RefreshScope
public class MsgUtil {

	@Value("${msg.sendSmsUrl}")
	private String sendSmsUrl;
	@Value("${msg.username}")
	private String username;
	@Value("${msg.password}")
	private String password;


	public String sendSms(String phone,String msg){

//		String url = "https://api.mix2.zthysms.com/v2/sendSms";
		Long tKey = Instant.now().getEpochSecond();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("username",username);
		jsonObject.put("password", DigestUtil.md5Hex(DigestUtil.md5Hex(password)+tKey));
		jsonObject.put("tKey",tKey);

		jsonObject.put("mobile",phone);
		jsonObject.put("content",msg);
		log.info("{}短信请求数数据：{}",tKey,jsonObject);
		String data = HttpUtil.createPost(sendSmsUrl)
				.header("Content-Type","application/json")
				.body(jsonObject.toJSONString())
				.execute()
				.body();
		log.info("{}响应数数据：{}",tKey,data);
		return null;
	}

//	public static void main(String[] args) {
//		sendSms("18325715421","【薪灵薪】您的验证码是1231");
//	}

}
